<script setup lang="ts">
import TableView from '@/components/TableView.vue'
import {
  NButton,
  NDataTable,
  NForm,
  NFormItem,
  NPagination,
  useMessage,
  type DataTableColumns,
  NSpace,
  NInput,
  NSelect,
  NModal
} from 'naive-ui'
import { h, ref, reactive, watch, onDeactivated } from 'vue'
import router from '@/router'
import { useRoute } from 'vue-router'
import { sendRequest } from '@/utils/api'
import { useViewerStore } from '../Common/store'
import VDialogContent from '@/views/OA/Common/DialogContent.vue'

const message = useMessage()
const route = useRoute()
const useStore = useViewerStore()

const rowKey: any = (e: any) => e.id || ''

const columns: DataTableColumns = [
  {
    title: '卷内档号/档号',
    key: 'caseArchiveNumber',
    width: '10rem',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    },
    render(e: any) {
      return e.type == '非文书' ? e.innerArchiveNumber : e.archiveNumber
    }
  },
  {
    title: '文件编号',
    key: 'fileNumber',
    width: '10rem',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  {
    title: '文件题名',
    key: 'fileTitle',
    width: '10rem',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  {
    title: '责任者',
    key: 'responsiblePerson',
    width: '10rem',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  {
    title: '文件日期',
    key: 'fileDate',
    width: '10rem',
    align: useStore.isCenter
  },
  {
    title: '文件页数',
    key: 'pageCount',
    width: '5rem',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  {
    title: '备注',
    key: 'notes',
    width: '10rem',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  // {
  //   title: '密级',
  //   key: 'securityLevel',
  //   width: '5rem',
  //   align: useStore.isCenter,
  //   ellipsis: {
  //     tooltip: {
  //       contentStyle: {
  //         maxWidth: '400rem'
  //       }
  //     }
  //   }
  // },
  {
    title: '创建时间',
    key: 'createTime',
    width: '10rem',
    align: useStore.isCenter
  },
  {
    title: '操作',
    key: 'action',
    width: '10rem',
    align: useStore.isCenter,
    fixed: 'right',
    render: (e: any) => [
      h(
        NButton,
        { type: 'primary', quaternary: true, onClick: () => handleEdit(e) },
        {
          default: () => '编辑'
        }
      ),
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          color: useStore.color['delete'],
          onClick: () => handleDelete([e?.id])
        },
        {
          default: () => '删除'
        }
      )
    ]
  }
]

//筛选条件
const formValue = reactive({
  archiveId: null,
  archiveName: null,
  isSecret: null
})
const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  showSizePicker: true,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  onChange: (page: number) => {
    paginationReactive.page = page
    queryTableData()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    queryTableData()
  }
})

const isSecretList = [
  {
    value: '涉密',
    label: '涉密'
  },
  {
    value: '非涉密',
    label: '非涉密'
  }
]

//新增
const handleAdd = () => {
  router.push({
    path: '/OA/File/MaintenanceAdd'
  })
}

//非文书档案批量导入
const handleImport = (val: any) => {
  router.push({
    path: '/OA/File/MaintenanceImport',
    query: {
      id: val.id
    }
  })
}

//文书档案批量导入
const handleBookImport = (val: any) => {
  router.push({
    path: '/OA/File/MaintenanceBookImport',
    query: {
      id: val.id
    }
  })
}

//编辑
const handleEdit = (val: any) => {
  router.push({
    path: '/OA/File/MaintenanceEdit',
    query: {
      id: val.id
    }
  })
}

//删除
const handleDelete = (val: any) => {
  modalId.value = val
  modalVisible.value = true
}

//删除弹窗
const modalVisible = ref(false)
const modalId = ref()
const againDelete = async () => {
  const [error, res] = await sendRequest({
    module: 'file',
    api: 'maintenanceDelete',
    override: {
      data: modalId.value
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  modalVisible.value = false
  queryTableData(true)
}
//表格数据
const tableLoading = ref(false)
const tableData: any = ref([])
const queryTableData = async (resetPage = false) => {
  if (resetPage) paginationReactive.page = 1
  tableLoading.value = true
  const [error, res] = await sendRequest({
    module: 'file',
    api: 'maintenanceList',
    override: {
      params: {
        ...formValue,
        page: paginationReactive.page,
        limit: paginationReactive.pageSize
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  paginationReactive.itemCount = res.data?.totalCount
  const tmp: any = res.data
  tableData.value = tmp?.list
}

const keyUpDown = (e: any) => e.key == 'Enter' && (paginationReactive.page = 1,queryTableData(true))
onDeactivated(() => window.removeEventListener('keyup', keyUpDown, false))


watch(
  () => route.path,
  async () => {
    if (route.path == '/OA/File/FileMaintenance') {
      await queryTableData()
      window.addEventListener('keyup', keyUpDown, false)
    }
  },
  { deep: true, immediate: true }
)
</script>

<template>
  <TableView class="table">
    <template #form>
      <NSpace justify="space-between">
        <NForm :show-feedback="false" label-placement="left" inline>
          <NFormItem label="卷内档号:">
            <NInput clearable placeholder="请输入卷内档号" v-model:value="formValue.archiveId" />
          </NFormItem>
          <NFormItem label="档案名称:">
            <NInput clearable placeholder="请输入档案名称" v-model:value="formValue.archiveName" />
          </NFormItem>
          <!-- <NFormItem label="是否涉密:">
            <NSelect clearable placeholder="请选择" style="width: 200rem" :options="isSecretList"
              v-model:value="formValue.isSecret"  />
          </NFormItem> -->
          <NFormItem>
            <NButton type="primary" style="width: 78px; margin-right: 40rem" ghost @click="queryTableData(true)">
              查询
            </NButton>
          </NFormItem>
        </NForm>
        <NSpace align="end">
          <NButton type="primary" @click="handleAdd" ghost>
            档案录入
          </NButton>
          <NButton type="primary" @click="handleImport" ghost>
            非文书档案批量导入
          </NButton>
          <NButton type="primary" @click="handleBookImport" ghost>
            文书档案批量导入
          </NButton>
        </NSpace>
      </NSpace>
    </template>

    <template #table="props">
      <NDataTable v-bind="props" :bordered="false" :row-key="rowKey" :columns="columns" :data="tableData"
        :loading="tableLoading" />
    </template>

    <template #pagination>
      <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
    </template>
  </TableView>

  <NModal :auto-focus="false" v-model:show="modalVisible">
    <VDialogContent @close="modalVisible = false" @submit="againDelete" title="删除提醒">
      <div style="width: 300rem; height: 100rem; text-align: center; line-height: 100rem">
        您确定要删除吗?
      </div>
    </VDialogContent>
  </NModal>
</template>

<style scoped lang="scss">
.table {
  height: 100%;
  padding: 29px 30px 42px;
}
</style>
